package fem2d;

import java.util.ArrayList;

import fem2.AbstractStructuralStaticDemo;
import fem2.Constraint;
import fem2.Element;
import fem2.GravityLoad;
import fem2.Load;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.MeshGenerator;
import fem2.MeshPart;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.Node;
import fem2.element.StructuralElement;
import fem2.enu.State;
import fem2.material.StVenantKirchhoffMM;
import fem2.pre_and_post.GidMeshGenerator;

/**
 * slope analysis with normal element and StVenantKrirchoff material
 * 
 * @author hbui
 * 
 */
public class SlopeStability extends AbstractStructuralStaticDemo {

	public SlopeStability() {
		projectDir = "/home/hbui/kratos_janosch";
		projectName = "slope-stability.gid";
		// meshFileName = "hyplas-mesh.msh"; // 8-node quadrilateral mesh //bad
		// result
		// meshFileName = "mesh1.msh"; // 4-node quadrilateral mesh
		// meshFileName = "mesh2.msh"; // 6-node triangle mesh //good result
		// meshFileName = "mesh3.msh"; // 3-node triangle mesh
		meshFileName = "abaqus-mesh.msh"; // 8-node quadrilateral mesh //good
		// result
		// meshFileName = "abaqus-q4-mesh.msh"; // 4-node quadrilateral mesh
		// //good
		// result
		// meshFileName = "mesh4.msh"; // 6-node triangle mesh //good result
	}

	@Override
	public Mesh createMesh() {
		String fn = projectDir + '/' + projectName + '/' + meshFileName;
		MeshGenerator mg = new GidMeshGenerator(fn);
		Mesh mesh = mg.getMesh(2);
		return mesh;
	}

	@Override
	public Model createConditions(Model m) {
		Mesh mesh = m.getMesh();

		Constraint c1 = new Constraint(false, false);
		Constraint c2 = new Constraint(false, true);

		ArrayList<Node> bottom = MeshUtilities.seekNodesOnSurface(mesh, 0.0, 1.0, 0.0);
		ArrayList<Node> left = MeshUtilities.seekNodesOnSurface(mesh, 1.0, 0.0, 0.0);
		ArrayList<Node> right = MeshUtilities.seekNodesOnSurface(mesh, 1.0, 0.0, -75.0);

		mesh.addConstraint(c1, bottom);
		mesh.addConstraint(c2, left);
		mesh.addConstraint(c2, right);

		double thickness = 1.0;
		double density = 2.038735e3 * thickness;
		for (int i = 0; i < mesh.countFaces(); i++) {
			Load f = new GravityLoad(mesh.getFace(i), density);
			m.addLoad(f);
		}

		return m;
	}

	@Override
	public MaterialModel createMaterial(Model m) {
		double E = 20e6;
		double nu = 0.49;
		double thickness = 1.0;
		State ss = State.PLANE_STRAIN;
		double rho = 0.0;

		return new StVenantKirchhoffMM(E, nu, thickness, rho, ss);
	}

	@Override
	public Element createElement(MeshPart mp, MaterialModel mm) {
		StructuralElement e = new StructuralElement(mp, mm);
		// e.setIntegrationType(IntegrationType.REDUCED_INTEGRATION);
		return e;
	}

	public static void main(String[] args) {
		AbstractStructuralStaticDemo demo = new SlopeStability();

		demo.run();
	}
}
